import {
    createRouter,
    createWebHistory
} from 'vue-router'
import {
    getToken,
    setToken,
    removeToken
} from "@/utils/auth";
import {
    Toast,
    Notify
} from 'vant';
import store from '@/store'; // ⚪️ 引入状态管理文件夹
// import {
//     defineAsyncComponent
// } from 'vue'
// const path = require('path')

const isProduction = process.env.NODE_ENV === 'production';
const basePath = isProduction ? '/' : '/'

const router = createRouter({
    history: createWebHistory(basePath), // 如果是根目录部署请配置为 /
    routes: [{
        path: '/',
        redirect: '/Home',
    },
    // route -> routes
    { //首页
        path: '/',
        name: 'layout',
        component: (() => import('../views/layout/index.vue')),
        children: [{
            path: "/Home",
            name: "Home",
            component: (() => import('../views/Home.vue')),
            meta: {
                title: '首页',
                isAuthRequired: true // 授权才能访问 标志
            }
        },
        { //商品投资
            path: '/Commodity',
            name: 'Commodity',
            component: (() => import('../views/Commodity.vue')),
            meta: {
                title: '商品投资',
                isAuthRequired: true // 授权才能访问 标志
            }
        },
        { //全品
            path: '/AllCategories',
            name: 'AllCategories',
            component: (() => import('../views/AllCategories.vue')),
            meta: {
                title: '全品',
                isAuthRequired: true // 授权才能访问 标志
            }
        },
        { //我的
            path: '/my',
            name: 'my',
            component: (() => import('../views/my.vue')),
            meta: {
                title: '我的',
                isAuthRequired: true // 授权才能访问 标志
            }
        },
        ],
    },
    {
        path: '/login',
        name: 'login',
        component: (() => import('../views/login/index.vue')),
        meta: {
            title: '登录',
            isAuthRequired: false // 授权才能访问 标志
        }
    },
    {
        path: '/register',
        name: 'register',
        component: (() => import('../views/register/index.vue')),
        meta: {
            title: '注册',
            isAuthRequired: false // 授权才能访问 标志
        }
    },
    {
        path: '/share',
        name: 'share',
        component: (() => import('../views/share/index.vue')),
        meta: {
            title: '分享',
            isAuthRequired: false // 授权才能访问 标志
        }
    },
    {
        path: '/download',
        name: 'download',
        component: (() => import('../views/download/index.vue')),
        meta: {
            title: '下载',
            isAuthRequired: false // 授权才能访问 标志
        }
    },
    { //抽奖
        path: '/luckdraw',
        name: 'luckdraw',
        component: (() => import('../views/luckdraw/index.vue')),
        meta: {
            title: '抽奖',
            isAuthRequired: true // 授权才能访问 标志
        }
    },
    { //钱包
        path: '/Capital',
        name: 'Capital',
        component: (() => import('../views/Capital/index.vue')),
        meta: {
            title: '钱包',
            isAuthRequired: true // 授权才能访问 标志
        }
    },

    { //提现
        path: '/withdrawal',
        name: 'withdrawal',
        component: (() => import('../views/withdrawal/index.vue')),
        meta: {
            title: '提现',
            isAuthRequired: true // 授权才能访问 标志
        }
    },
    { //我的资料
        path: '/myinformation',
        name: 'myinformation',
        component: (() => import('../views/myinformation/index.vue')),
        meta: {
            title: '我的资料',
            isAuthRequired: true // 授权才能访问 标志
        }
    },
    { //我的投资
        path: '/myinvestment',
        name: 'myinvestment',
        component: (() => import('../views/myinvestment/index.vue')),
        meta: {
            title: '我的投资',
            isAuthRequired: true // 授权才能访问 标志
        }
    },
    { //我的团队
        path: '/myteam',
        name: 'myteam',
        component: (() => import('../views/myteam/index.vue')),
        meta: {
            title: '我的团队',
            isAuthRequired: true // 授权才能访问 标志
        }
    },
    { //我的团队列表
        path: '/myteamlist',
        name: 'myteamlist',
        component: (() => import('../views/myteamlist/index.vue')),
        meta: {
            title: '我的团队列表',
            isAuthRequired: true // 授权才能访问 标志
        }
    },
    { //商品详情
        path: '/ProductDetails',
        name: 'ProductDetails',
        component: (() => import('../views/ProductDetails.vue')),
        meta: {
            title: '商品详情',
            isAuthRequired: true // 授权才能访问 标志
        }
    },
    { //商品详情 h5
        path: '/ProductDetailsH5',
        name: 'ProductDetailsH5',
        component: (() => import('../views/ProductDetailsH5.vue')),
        meta: {
            title: '商品详情',
            isAuthRequired: true // 授权才能访问 标志
        }
    },

    { //积分中心
        path: '/pointscenter',
        name: 'pointscenter',
        component: (() => import('../views/pointscenter/index.vue')),
        meta: {
            title: '积分中心',
            isAuthRequired: true // 授权才能访问 标志
        }
    },
    { //收货地址
        path: '/address',
        name: 'address',
        component: (() => import('../views/address/index.vue')),
        meta: {
            title: '收货地址',
            isAuthRequired: true // 授权才能访问 标志
        }
    },
    { //我的订单
        path: '/myorder',
        name: 'myorder',
        component: (() => import('../views/myorder/index.vue')),
        meta: {
            title: '我的订单',
            isAuthRequired: true // 授权才能访问 标志
        }
    },
    { //充值
        path: '/recharge',
        name: 'recharge',
        component: (() => import('../views/recharge/index.vue')),
        meta: {
            title: '充值',
            isAuthRequired: true // 授权才能访问 标志
        }
    },
    ],
})
router.beforeEach((to, from, next) => {
    var token = getToken()
    // console.log(token)
    // 如果需要授权，但当前状态为假，跳转到 登录页面
    // 判断原元素中的isAuthRequired 和 user下面的isLogin状态是否为false
    if (to.meta.isAuthRequired && !token) {
        Notify('您还没有登录，请先登录！')
        return next('/login') // 如果没有登录，跳转到 login
    } else {
        next() // 走到这里，证明已登录，直接放行
    }
    document.title = to.meta.title
})
router.afterEach((to, from) => {
    // console.log('afterEach')
})

export default router